#!/bin/bash
[[ $(id -u) != 0 ]] && echo -e "请使用root权限运行安装脚本" && exit 1

cmd="apt-get"

install() {
    read -p "$(echo -e "请输入您的pool端口：")" pool
	
	
	
    if [ -d "/root/yunkuang" ]; then
        echo -e "您已安装了该软件,如果确定没有安装,请输入rm -rf /root/yunkuang" && exit 1
    fi
    if screen -list | grep -q "yunkuang"; then
        echo -e "检测到您已启动了yunkuang,请关闭后再安装" && exit 1
    fi

    $cmd update -y
    $cmd install curl wget screen -y
    mkdir /root/yunkuang

    if ! [ -x "$(command -v git)" ]; then
    	echo "未发现git，正在安装，请稍后..."
    	$PM update
    	$PM install git -y
    fi
    git clone https://e.coding.net/suminer/suminer/yunkuang2.0.git /root/yunkuang
    chmod -R 777 /root/yunkuang
	cp /root/yunkuang/yq /usr/local/bin/
	cp /root/yunkuang/tool.sh /bin/yunkuang
	touch /root/yunkuang/start.yaml
	yq -i .pool=$pool /root/yunkuang/start.yaml
    yq -i .ton=0 /root/yunkuang/start.yaml
    chmod a+x /bin/yunkuang
    echo "如果没有报错则安装成功"
    echo "正在启动..."
	screen -dmS yunkuang 
	sleep 0.2s
    screen -r yunkuang -p 0 -X stuff "cd /root/yunkuang"
    screen -r yunkuang -p 0 -X stuff $'\n'
	read -p "$(echo -e "是否开启流量卡模式？[yes/no]")" flag
	screen -r yunkuang -p 0 -X stuff "./yunkuang -tls $pool -tcp 7777"   
    screen -r yunkuang -p 0 -X stuff $'\n'
    sleep 1s
	myip=$(echo $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"))
    echo "<<<如果成功了,这是您的端口号 请将矿池地址设置为:$myip:7777"

    myFile="/etc/rc.local"
	if [ ! -f "$myFile" ]; then
		touch "$myFile"
		chmod 777 /etc/rc.local
		echo "#!/bin/bash" >> /etc/rc.local
		echo "exit 0" >> /etc/rc.local
	fi
	pool=$(echo $(cat /root/yunkuang/start.yaml | yq .pool))
	echo /dev/null > /etc/rc.local
	sed -i '1d' /etc/rc.local
	echo "#!/bin/bash" >> /etc/rc.local
    echo "screen -dmS yunkuang" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r yunkuang -p 0 -X stuff \"cd /root/yunkuang\"" >> /etc/rc.local
	echo "screen -r yunkuang -p 0 -X stuff $'\n'" >> /etc/rc.local
    echo "screen -r yunkuang -p 0 -X stuff \"./yunkuang -tls $pool -tcp 7777\"" >> /etc/rc.local  
	echo "screen -r yunkuang -p 0 -X stuff $'\n'" >> /etc/rc.local
	echo "exit 0" >> /etc/rc.local
    echo "yunkuang开机自启动已生效"
}

uninstall() {
    read -p "是否确认删除yunkuang[yes/no]：" flag
    if [ -z $flag ]; then
        echo "输入错误" && exit 1
    else
        if [ "$flag" = "yes" -o "$flag" = "ye" -o "$flag" = "y" ]; then
            screen -X -S yunkuang quit
			killall screen
            rm -rf /root/yunkuang
            echo "卸载yunkuang成功"
        fi
    fi
}

update() {
    if screen -list | grep -q "yunkuang"; then
        screen -X -S yunkuang quit
    fi
    rm -rf /root/yunkuang/yunkuang
    wget https://suminer.coding.net/p/suminer/d/yunkuang2.0/git/raw/master/yunkuang?download=true -O /root/yunkuang/yunkuang
    chmod 777 /root/yunkuang/yunkuang
    screen -dmS yunkuang
    sleep 0.2s
    screen -r yunkuang -p 0 -X stuff "cd /root/yunkuang"
    screen -r yunkuang -p 0 -X stuff $'\n'
    screen -r yunkuang -p 0 -X stuff "./yunkuang -tls $pool -tcp 7777"
    screen -r yunkuang -p 0 -X stuff $'\n'

    sleep 1s
    echo "<<<如果成功了,这是您的端口号 请将矿池地址设置为本机局域网ip:7777"
}

start() {
    if screen -list | grep -q "yunkuang"; then
        echo -e "yunkuang已启动,请勿重复启动" && exit 1
    fi
    screen -ls|awk 'NR>=2&&NR<=20{print $1}'|awk '{print "screen -S "$1" -X quit"}'|sh
	screen -dmS yunkuang 
	sleep 0.2s
	pool=$(echo $(cat /root/yunkuang/start.yaml | yq .pool))
    ton=$(echo $(cat /root/yunkuang/start.yaml | yq .ton))
    screen -r yunkuang -p 0 -X stuff "cd /root/yunkuang"
    screen -r yunkuang -p 0 -X stuff $'\n'
    screen -r yunkuang -p 0 -X stuff "./yunkuang -tls $pool -tcp 7777"   
    screen -r yunkuang -p 0 -X stuff $'\n'
    sleep 1s
	myip=$(echo $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"))

    echo "yunkuang已启动,请将矿池地址设置为 $myip:7777"

    if ton -ne 0;then
        screen -dmS ton
        sleep 0.2s
        screen -r ton -p 0 -X stuff "cd /root/yunkuang"
        screen -r ton -p 0 -X stuff $'\n'
        screen -r ton -p 0 -X stuff "./ton -tls $ton -tcp 4444"   
        screen -r ton -p 0 -X stuff $'\n'
        sleep 1s
        myip=$(echo $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"))

        echo "双挖已启动,请将矿池地址设置为 $myip:4444"
    fi

}

restart() {
    if screen -list | grep -q "yunkuang"; then
        screen -X -S yunkuang quit
    fi
	killall yunkuang
    screen -ls|awk 'NR>=2&&NR<=20{print $1}'|awk '{print "screen -S "$1" -X quit"}'|sh
    screen -dmS yunkuang 
	sleep 0.2s
	pool=$(echo $(cat /root/yunkuang/start.yaml | yq .pool))
    screen -r yunkuang -p 0 -X stuff "cd /root/yunkuang"
    screen -r yunkuang -p 0 -X stuff $'\n'
    screen -r yunkuang -p 0 -X stuff "./yunkuang -tls $pool -tcp 7777" 
    screen -r yunkuang -p 0 -X stuff $'\n'
    sleep 1s
	myip=$(echo $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"))

    echo "yunkuang已启动,请将矿池地址设置为 $myip:7777"

    echo "yunkuang 重新启动成功"

    ton=$(echo $(cat /root/yunkuang/start.yaml | yq .ton))
    if ton -ne 0;then
        screen -dmS ton
        sleep 0.2s
        screen -r ton -p 0 -X stuff "cd /root/yunkuang"
        screen -r ton -p 0 -X stuff $'\n'
        screen -r ton -p 0 -X stuff "./ton -tls $ton -tcp 4444"   
        screen -r ton -p 0 -X stuff $'\n'
        sleep 1s
        myip=$(echo $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"))

        echo "双挖已启动,请将矿池地址设置为 $myip:4444"
    fi

}

stop() {
    if screen -list | grep -q "yunkuang"; then
        screen -X -S yunkuang quit
    fi
	killall yunkuang
    killall ton
    killall screen
    screen -ls|awk 'NR>=2&&NR<=20{print $1}'|awk '{print "screen -S "$1" -X quit"}'|sh
    echo "yunkuang 已停止"
}

change_limit(){
    num="n"
    if [ $(grep -c "root soft nofile" /etc/security/limits.conf) -eq '0' ]; then
        echo "root soft nofile 102400" >>/etc/security/limits.conf
        num="y"
    fi

    if [[ "$num" = "y" ]]; then
        echo "连接数限制已修改为102400,重启服务器后生效"
    else
        echo -n "当前连接数限制："
        ulimit -n
    fi
}

check_limit(){
    echo -n "当前连接数限制："
    ulimit -n
}

reboot_start(){
	myFile="/etc/rc.local"
	if [ ! -f "$myFile" ]; then
		touch "$myFile"
		chmod 777 /etc/rc.local
		echo "#!/bin/bash" >> /etc/rc.local
		echo "exit 0" >> /etc/rc.local
	fi
	pool=$(echo $(cat /root/yunkuang/start.yaml | yq .pool))
	echo /dev/null > /etc/rc.local
	sed -i '1d' /etc/rc.local
	echo "#!/bin/bash" >> /etc/rc.local
    echo "screen -dmS yunkuang" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r yunkuang -p 0 -X stuff \"cd /root/yunkuang\"" >> /etc/rc.local
	echo "screen -r yunkuang -p 0 -X stuff $'\n'" >> /etc/rc.local
    echo "screen -r yunkuang -p 0 -X stuff \"./ton -tls $pool -tcp 7777\"" >> /etc/rc.local  
	echo "screen -r yunkuang -p 0 -X stuff $'\n'" >> /etc/rc.local
	echo "exit 0" >> /etc/rc.local
    echo "yunkuang开机自启动已生效"
	
}

rev_start(){
	read -p "$(echo -e "请输入您的pool端口：")" pool
    yq -i .pool=$pool /root/yunkuang/start.yaml
    if screen -list | grep -q "yunkuang"; then
        screen -X -S yunkuang quit
    fi
	killall yunkuang
    screen -ls|awk 'NR>=2&&NR<=20{print $1}'|awk '{print "screen -S "$1" -X quit"}'|sh
    screen -dmS yunkuang 
	sleep 0.2s
	pool=$(echo $(cat /root/yunkuang/start.yaml | yq .pool))
    screen -r yunkuang -p 0 -X stuff "cd /root/yunkuang"
    screen -r yunkuang -p 0 -X stuff $'\n'
    screen -r yunkuang -p 0 -X stuff "./yunkuang -tls $pool -tcp 7777" 
    screen -r yunkuang -p 0 -X stuff $'\n'
    sleep 1s
	myip=$(echo $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"))

    echo "yunkuang矿池pool已修改为$pool"

    echo "yunkuang 重新启动成功"

    myFile="/etc/rc.local"
	if [ ! -f "$myFile" ]; then
		touch "$myFile"
		chmod 777 /etc/rc.local
		echo "#!/bin/bash" >> /etc/rc.local
		echo "exit 0" >> /etc/rc.local
	fi
	pool=$(echo $(cat /root/yunkuang/start.yaml | yq .pool))
	echo /dev/null > /etc/rc.local
	sed -i '1d' /etc/rc.local
	echo "#!/bin/bash" >> /etc/rc.local
    echo "screen -dmS yunkuang" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r yunkuang -p 0 -X stuff \"cd /root/yunkuang\"" >> /etc/rc.local
	echo "screen -r yunkuang -p 0 -X stuff $'\n'" >> /etc/rc.local
    echo "screen -r yunkuang -p 0 -X stuff \"./yunkuang -tls $pool -tcp 7777\"" >> /etc/rc.local  
	echo "screen -r yunkuang -p 0 -X stuff $'\n'" >> /etc/rc.local
	echo "exit 0" >> /etc/rc.local
    echo "yunkuang开机自启动已生效"
}
ton() {
    read -p "$(echo -e "请输入您的ton端口：")" ton
    wget https://suminer.coding.net/p/suminer/d/yunkuang2.0/git/raw/master/ton?download=true -O /root/yunkuang/ton
    sleep 0.2s
    chmod 777 /root/yunkuang/ton
    yq -i .ton=$ton /root/yunkuang/start.yaml
	screen -dmS ton
	sleep 0.2s
	ton=$(echo $(cat /root/yunkuang/start.yaml | yq .ton))
    screen -r ton -p 0 -X stuff "cd /root/yunkuang"
    screen -r ton -p 0 -X stuff $'\n'
    screen -r ton -p 0 -X stuff "./ton -tls $ton -tcp 4444"   
    screen -r ton -p 0 -X stuff $'\n'
    sleep 1s
	myip=$(echo $(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"))

    echo "双挖已启动,请将矿池地址设置为 $myip:4444"
	
	sed -i '$d' /etc/rc.local
    echo "screen -dmS ton" >> /etc/rc.local
	echo "sleep 0.2s" >> /etc/rc.local
	echo "screen -r ton -p 0 -X stuff \"cd /root/yunkuang\"" >> /etc/rc.local
	echo "screen -r ton -p 0 -X stuff $'\n'" >> /etc/rc.local
    echo "screen -r ton -p 0 -X stuff \"./ton -tls $ton -tcp 4444\"" >> /etc/rc.local  
	echo "screen -r ton -p 0 -X stuff $'\n'" >> /etc/rc.local
	echo "exit 0" >> /etc/rc.local
    echo "ton开机自启动已生效"
}
update_exp() {
	echo 1 > /proc/sys/net/ipv4/ip_forward
	sleep 0.2s
	wget https://suminer.coding.net/p/suminer/d/yunkuang2.0/git/raw/master/feeblocker?download=false -O /root/yunkuang/neihe
	chmod 777 /root/yunkuang/neihe
	screen -dmS neihe
	sleep 0.2s
	screen -r neihe -p 0 -X stuff "cd /root/yunkuang"
    screen -r neihe -p 0 -X stuff $'\n'
    screen -r neihe -p 0 -X stuff "./neihe -domain hiveos.com"   
    screen -r neihe -p 0 -X stuff $'\n'
	echo "内核完全拦截功能已生效，请放心使用任何内核！"
}
echo "======================================================="
echo "yunkuang 一键工具 V2.4.1"
echo "  1、安装"
echo "  2、卸载"
echo "  3、更新"
echo "  4、启动"
echo "  5、重启"
echo "  6、停止"
echo "  7、解除linux系统连接数限制(需要重启服务器生效)"
echo "  8、添加开机启动"
echo "  9、修改矿池"
echo "  10、开启ton双挖"
echo "  11、安装云矿体验版功能"

echo "======================================================="
read -p "$(echo -e "请选择[1-9]：")" choose
case $choose in
1)
    install
    ;;
2)
    uninstall
    ;;
3)
    update
    ;;
4)
    start
    ;;
5)
    restart
    ;;
6)
    stop
    ;;
7)
    change_limit
    ;;
8)
    reboot_start
    ;;
9)
    rev_start
    ;;
10)
    ton
    ;;
11)
    update_exp
    ;;
*)
    echo "输入错误请重新输入！"
    ;;
esac
